home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / zdevice.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  6KB  |  205 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zdevice.c */
  20. /* Device-related operators for Ghostscript */
  21. #include "ghost.h"
  22. #include "alloc.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "state.h"
  26. #include "gsmatrix.h"
  27. #include "gsstate.h"
  28. #include "gxdevice.h"
  29. #include "store.h"
  30.  
  31. /* <device> copydevice <newdevice> */
  32. int
  33. zcopydevice(register os_ptr op)
  34. {    gx_device *new_dev;
  35.     int code;
  36.     check_type(*op, t_device);
  37.     code = gs_copydevice(&new_dev, op->value.pdevice, &alloc_memory_procs);
  38.     if ( code < 0 ) return code;
  39.     make_tv(op, t_device, pdevice, new_dev);
  40.     return 0;
  41. }
  42.  
  43. /* <device> <y> <string> copyscanlines <substring> */
  44. int
  45. zcopyscanlines(register os_ptr op)
  46. {    os_ptr op1 = op - 1;
  47.     os_ptr op2 = op - 2;
  48.     gx_device *dev;
  49.     int code;
  50.     uint bytes_copied;
  51.     check_type(*op2, t_device);
  52.     dev = op2->value.pdevice;
  53.     check_type(*op1, t_integer);
  54.     if ( op1->value.intval < 0 || op1->value.intval > dev->height )
  55.         return_error(e_rangecheck);
  56.     check_write_type(*op, t_string);
  57.     code = gs_copyscanlines(dev, (int)op1->value.intval,
  58.         op->value.bytes, r_size(op), NULL, &bytes_copied);
  59.     if ( code < 0 )
  60.         return_error(e_typecheck);    /* not a memory device */
  61.     *op2 = *op;
  62.     r_set_size(op2, bytes_copied);
  63.     pop(2);
  64.     return 0;
  65. }
  66.  
  67. /* - currentdevice <device> */
  68. int
  69. zcurrentdevice(register os_ptr op)
  70. {    gx_device *dev = gs_currentdevice(igs);
  71.     push(1);
  72.     make_tv(op, t_device, pdevice, dev);
  73.     return 0;
  74. }
  75.  
  76. /* <device> devicename <string> */
  77. int
  78. zdevicename(register os_ptr op)
  79. {    const char *dname;
  80.     int code;
  81.     check_type(*op, t_device);
  82.     dname = gs_devicename(op->value.pdevice);
  83.     code = string_to_ref(dname, op, "devicename");
  84.     if ( code < 0 ) return code;
  85.     return 0;
  86. }
  87.  
  88. /* <device> <matrix> deviceinitialmatrix <matrix> */
  89. int
  90. zdeviceinitialmatrix(register os_ptr op)
  91. {    int code = write_matrix(op);
  92.     if ( code < 0 ) return code;
  93.     check_type(op[-1], t_device);
  94.     gs_deviceinitialmatrix(op[-1].value.pdevice, (gs_matrix *)op->value.refs);
  95.     op[-1] = *op;
  96.     pop(1);
  97.     return 0;
  98. }
  99.  
  100. /* - flushpage - */
  101. int
  102. zflushpage(register os_ptr op)
  103. {    return gs_flushpage(igs);
  104. }
  105.  
  106. /* <int> getdevice <device> */
  107. int
  108. zgetdevice(register os_ptr op)
  109. {    gx_device *dev;
  110.     check_type(*op, t_integer);
  111.     if ( op->value.intval != (int)(op->value.intval) )
  112.         return_error(e_rangecheck);    /* won't fit in an int */
  113.     dev = gs_getdevice((int)(op->value.intval));
  114.     if ( dev == 0 )        /* index out of range */
  115.         return_error(e_rangecheck);
  116.     make_tv(op, t_device, pdevice, dev);
  117.     return 0;
  118. }
  119.  
  120. /* <matrix> <width> <height> <palette> makeimagedevice <device> */
  121. int
  122. zmakeimagedevice(register os_ptr op)
  123. {    gs_matrix imat;
  124.     gx_device *new_dev;
  125.     const byte *colors;
  126.     int num_colors;
  127.     int code;
  128.     static const gs_memory_procs mprocs = { alloc, alloc_free };
  129.     check_type(op[-2], t_integer);    /* width */
  130.     check_type(op[-1], t_integer);    /* height */
  131.     if ( r_has_type(op, t_null) )    /* true color */
  132.        {    colors = 0;
  133.         num_colors = -24;    /* 24-bit true color */
  134.        }
  135.     else
  136.        {    check_type(*op, t_string);    /* palette */
  137.         if ( r_size(op) > 3*256 )
  138.             return_error(e_rangecheck);
  139.         colors = op->value.bytes;
  140.         num_colors = r_size(op);
  141.        }
  142.     if (    (ulong)(op[-2].value.intval) > max_uint >> 1 ||
  143.         (ulong)(op[-1].value.intval) > max_uint >> 1
  144.        )
  145.         return_error(e_rangecheck);
  146.     if ( (code = read_matrix(op - 3, &imat)) < 0 )
  147.         return code;
  148.     /* Everything OK, create device */
  149.     code = gs_makeimagedevice(&new_dev, &imat,
  150.                   (int)op[-2].value.intval,
  151.                   (int)op[-1].value.intval,
  152.                   colors, num_colors, &mprocs);
  153.     if ( code == 0 )
  154.        {    make_tv(op - 3, t_device, pdevice, new_dev);
  155.         pop(3);
  156.        }
  157.     return code;
  158. }
  159.  
  160. /* - nulldevice - */
  161. int
  162. znulldevice(register os_ptr op)
  163. {    gs_nulldevice(igs);
  164.     return 0;
  165. }
  166.  
  167. /* <num_copies> <flush_bool> .outputpage - */
  168. int
  169. zoutputpage(register os_ptr op)
  170. {    int code;
  171.     check_type(op[-1], t_integer);
  172.     check_type(*op, t_boolean);
  173.     code = gs_output_page(igs, (int)op[-1].value.intval, op->value.index);
  174.     if ( code < 0 ) return code;
  175.     pop(2);
  176.     return 0;
  177. }
  178.  
  179. /* <device> setdevice - */
  180. int
  181. zsetdevice(register os_ptr op)
  182. {    int code;
  183.     check_type(*op, t_device);
  184.     code = gs_setdevice(igs, op->value.pdevice);
  185.     if ( code == 0 ) pop(1);
  186.     return code;
  187. }
  188.  
  189. /* ------ Initialization procedure ------ */
  190.  
  191. op_def zdevice_op_defs[] = {
  192.     {"1copydevice", zcopydevice},
  193.     {"3copyscanlines", zcopyscanlines},
  194.     {"0currentdevice", zcurrentdevice},
  195.     {"2deviceinitialmatrix", zdeviceinitialmatrix},
  196.     {"1devicename", zdevicename},
  197.     {"0flushpage", zflushpage},
  198.     {"1getdevice", zgetdevice},
  199.     {"4makeimagedevice", zmakeimagedevice},
  200.     {"0nulldevice", znulldevice},
  201.     {"2.outputpage", zoutputpage},
  202.     {"1setdevice", zsetdevice},
  203.     op_def_end(0)
  204. };
  205.